package frame;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Client.MyClient;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.Image;

import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Register extends JFrame {
	private String uname;	//用户名
	private String upassword;//密码
	private JPanel contentPane;
	private MyClient myClient;
	private JTextField text_username;
	private JPasswordField text_password;
	private JPasswordField text_next_password;
	private JTextField text_question;
	private JTextField text_answer;
public void setUname(String uname) {
	this.uname=uname;
}
public void setUpassword(String upassword) {
	this.upassword=upassword;
}
public String getUname() {
	return uname;
}
public String getUpassword() {
	return upassword;
}

	/**
	 * Create the frame.
	 */
	public Register(MyClient myClient) {
		this.myClient=myClient;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 830, 525);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setLocationRelativeTo(null);
		contentPane.setLayout(null);
		

		JLabel font_start = new JLabel("");
		font_start.setIcon(new ImageIcon("photo\\username.png"));
		font_start.setBounds(103, 13, 233, 65);
		contentPane.add(font_start);
		
		
		ImageIcon imageIconSource5 = new ImageIcon("photo\\password.png");
	    Image image5 = imageIconSource5.getImage();
	    Image tempImage5 = image5.getScaledInstance(200,70,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon5 = new ImageIcon(tempImage5);
	    
		JLabel font_start_2 = new JLabel("");
		font_start_2.setIcon(imageIcon5);
		font_start_2.setBounds(103, 91, 233, 65);
		contentPane.add(font_start_2);
		
		JLabel font_start_3 = new JLabel("");
		font_start_3.setIcon(new ImageIcon("photo\\two_password.png"));
		font_start_3.setBounds(103, 169, 241, 65);
		contentPane.add(font_start_3);
		
		text_username = new JTextField();
		text_username.setOpaque(false);
		text_username.setForeground(Color.WHITE);
		text_username.setFont(new Font("宋体", Font.BOLD, 25));
		text_username.setColumns(10);
		text_username.setBackground(Color.BLACK);
		text_username.setBounds(401, 28, 217, 34);
		contentPane.add(text_username);
		
		text_password = new JPasswordField();
		text_password.setForeground(Color.WHITE);
		text_password.setOpaque(false);
		text_password.setFont(new Font("宋体", Font.BOLD, 25));
		text_password.setBounds(401, 112, 217, 34);
		contentPane.add(text_password);
		
		text_next_password = new JPasswordField();
		text_next_password.setForeground(Color.WHITE);
		text_next_password.setOpaque(false);
		text_next_password.setFont(new Font("宋体", Font.BOLD, 25));
		text_next_password.setBounds(401, 188, 217, 34);
		contentPane.add(text_next_password);
		
		ImageIcon imageIconSource0 = new ImageIcon("photo\\input.png");
	    Image image0 = imageIconSource0.getImage();
	    Image tempImage0 = image0.getScaledInstance(300,70,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon0 = new ImageIcon(tempImage0);
	    
		JLabel lb1 = new JLabel("");
		lb1.setBounds(373, 95, 270, 65);
		lb1.setIcon(imageIcon0);
		contentPane.add(lb1);
		
		JLabel lb2 = new JLabel("");
		lb2.setBounds(373, 13, 270, 65);
		lb2.setIcon(imageIcon0);
		contentPane.add(lb2);
		
		
		ImageIcon imageIconSource1 = new ImageIcon("photo\\123.jpg");
	    Image image1 = imageIconSource1.getImage();
	    Image tempImage1 = image1.getScaledInstance(900,600,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon1 = new ImageIcon(tempImage1);
		
		JLabel lb1_1 = new JLabel("");
		lb1_1.setBounds(373, 173, 270, 65);
		lb1_1.setIcon(imageIcon0);
		contentPane.add(lb1_1);
		
		text_question = new JTextField();
		text_question.setOpaque(false);
		text_question.setForeground(Color.WHITE);
		text_question.setFont(new Font("宋体", Font.BOLD, 25));
		text_question.setColumns(10);
		text_question.setBackground(Color.BLACK);
		text_question.setBounds(401, 261, 217, 34);
		contentPane.add(text_question);
		
		JLabel lb2_1 = new JLabel("");
		lb2_1.setIcon(imageIcon0);
		lb2_1.setBounds(373, 246, 270, 65);
		contentPane.add(lb2_1);
		
		text_answer = new JTextField();
		text_answer.setOpaque(false);
		text_answer.setForeground(Color.WHITE);
		text_answer.setFont(new Font("宋体", Font.BOLD, 25));
		text_answer.setColumns(10);
		text_answer.setBackground(Color.BLACK);
		text_answer.setBounds(401, 337, 217, 34);
		contentPane.add(text_answer);
		
		JLabel lb2_2 = new JLabel("");
		lb2_2.setBounds(373, 322, 270, 65);
		lb2_2.setIcon(imageIcon0);
		contentPane.add(lb2_2);
		
		JLabel font_start2 = new JLabel("");
		font_start2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				uname=text_username.getText();
				upassword=new String(text_password.getPassword());
				String questionString =text_question.getText();
				String answringString=text_answer.getText();
				if(questionString.equals("")||answringString.equals("")||uname.equals("")||upassword.equals("")) {
					JOptionPane.showMessageDialog(Register.this, "请将信息补充完整");
				}else {
					if(upassword.equals(new String(text_next_password.getPassword()))) {
						
						register(uname,upassword,questionString,answringString);
					}else {
						JOptionPane.showMessageDialog(Register.this, "密码不一致请检查");
					}
				}
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				ImageIcon imageIconSource10 = new ImageIcon("photo\\reg2.png");
			    Image image10 = imageIconSource10.getImage();
			    Image tempImage10 = image10.getScaledInstance(200,80,Image.SCALE_DEFAULT);
			    ImageIcon imageIcon10 = new ImageIcon(tempImage10);
			    font_start2.setIcon(imageIcon10);
			}
			@Override
			public void mouseExited(MouseEvent e) {
				ImageIcon imageIconSource10 = new ImageIcon("photo\\reg1.png");
			    Image image10 = imageIconSource10.getImage();
			    Image tempImage10 = image10.getScaledInstance(200,80,Image.SCALE_DEFAULT);
			    ImageIcon imageIcon10 = new ImageIcon(tempImage10);
			    font_start2.setIcon(imageIcon10);
			}
		});
		
		ImageIcon imageIconSource7 = new ImageIcon("photo\\answer.png");
	    Image image7 = imageIconSource7.getImage();
	    Image tempImage7 = image7.getScaledInstance(100,90,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon7 = new ImageIcon(tempImage7);
	    
	    
		ImageIcon imageIconSource10 = new ImageIcon("photo\\reg1.png");
	    Image image10 = imageIconSource10.getImage();
	    Image tempImage10 = image10.getScaledInstance(200,80,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon10 = new ImageIcon(tempImage10);
	    
		font_start2.setBounds(250, 378, 233, 72);
		font_start2.setIcon(imageIcon10);
		contentPane.add(font_start2);
		
		

	    
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(imageIcon7);
		lblNewLabel.setBounds(85, 234, 73, 77);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(imageIcon7);
		lblNewLabel_1.setBounds(85, 324, 73, 77);
		contentPane.add(lblNewLabel_1);
		
		
		ImageIcon imageIconSource11 = new ImageIcon("photo\\question1.png");
	    Image image11 = imageIconSource11.getImage();
	    Image tempImage11 = image11.getScaledInstance(110,56,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon11 = new ImageIcon(tempImage11);
		JLabel label = new JLabel("");
		label.setIcon(imageIcon11);
		label.setForeground(Color.WHITE);
		label.setFont(new Font("宋体", Font.BOLD, 35));
		label.setBounds(185, 261, 151, 44);
		contentPane.add(label);
		
		imageIconSource7 = new ImageIcon("photo\\qusetion.png");
	    image7 = imageIconSource7.getImage();
	    tempImage7 = image7.getScaledInstance(105,105,Image.SCALE_DEFAULT);
	    imageIcon7 = new ImageIcon(tempImage7);
		
	    imageIconSource11 = new ImageIcon("photo\\answer1.png");
	    image11 = imageIconSource11.getImage();
	    tempImage11 = image11.getScaledInstance(110,56,Image.SCALE_DEFAULT);
	    imageIcon11 = new ImageIcon(tempImage11);
		JLabel label_1 = new JLabel("");
		label_1.setIcon(imageIcon11);
		label_1.setForeground(Color.WHITE);
		label_1.setFont(new Font("宋体", Font.BOLD, 35));
		label_1.setBounds(185, 333, 151, 38);
		contentPane.add(label_1);
		
		
		
		JLabel lbl_start = new JLabel("");
		lbl_start.setBounds(0, 0, 816, 450);
		lbl_start.setIcon(imageIcon1);
		contentPane.add(lbl_start);
	}
	public void isRegister(boolean a) {
		if(a) {
			JOptionPane.showMessageDialog(Register.this, "注册成功");
			this.dispose();
		}else {
			JOptionPane.showMessageDialog(Register.this, "注册失败请检查账号是否已经注册");
		}
	}
	public void register(String name,String passString,String question,String answringString) {
		myClient.register(uname, upassword,this,question,answringString);
	}
}
